<template>
  <!--
    待秒杀商品 功能列表

    @author wangyongkang ${authorEmail}
    @date 2022-03-17 23:48:40
  -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="itemId">
        <el-input
          v-model="queryParams.itemId"
          placeholder="请输入商品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态：">
        <el-select v-model="queryParams.isActive" placeholder="全部" clearable>
          <el-option
            v-for="item in ActiveOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ItemKillList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" prop="itemId" :formatter="formatterClassify"/>
      <el-table-column label="秒杀编号" align="center" prop="id" width="300">
      </el-table-column>
      <el-table-column label="秒杀数量" align="center" prop="total" />
      <el-table-column label="秒杀开始时间" align="center" prop="startTime">
        <template slot-scope="scope">
          <span>{{ parseDatetime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="秒杀结束时间" align="center" prop="endTime" >
        <template slot-scope="scope">
          <span>{{ parseDatetime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isActive" >
        <template slot-scope="scope">
          <div v-if="scope.row.isActive === 0" style="color:#ef084e">下架</div>
          <div v-if="scope.row.isActive === 1" style="color:green">上架</div>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="addTime">
        <template slot-scope="scope">
          <span>{{ parseDatetime(scope.row.addTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

<!--    选择商品对话框-->
    <el-dialog :title="productTitle" :visible.sync="itemOpen" width="1100px" append-to-body>
      <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChangeProduct">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="商品名称" align="left" prop="name" />
        <el-table-column label="商品编号" align="left" prop="id" >
        </el-table-column>
        <el-table-column label="库存" align="left" prop="stock" />
        <el-table-column label="状态" align="left" prop="isActive" >
          <template slot-scope="scope">
            <div v-if="scope.row.isActive === 0" style="color:#ef084e">下架</div>
            <div v-if="scope.row.isActive === 1" style="color:green">上架</div>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 50px;margin-top: 18px">
        <el-button type="warning" style="float: right" @click="addSecKill">加入秒杀</el-button>
      </div>
      <pagination
        v-show=true
        :total="this.itemTotal"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="itemList"
      />
    </el-dialog>



    <!-- 添加或修改待秒杀商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="秒杀数量" prop="total">-->
<!--          <el-input v-model="form.total" placeholder="请输入可被秒杀的总数" />-->
<!--        </el-form-item>-->
        <el-form-item label="库存" prop="total" style="width: 50%;">
          <el-input-number :min="0" type="number" v-model="form.total" placeholder="请输入库存"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable size="small"
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd"
            placeholder="秒杀开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small"
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd"
            placeholder="选择秒杀结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上架状态" prop="isActive">
          <el-radio v-model="form.isActive" :label="1" >上架</el-radio>
          <el-radio v-model="form.isActive" :label="0">下架</el-radio>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listItemKill,
  getItemKill,
  delItemKill,
  addItemKill,
  changeItemKillField,
  changeStatusItemKill,
  updateItemKill,
  exportItemKill,
  newAddSeckillGoods} from "@/api/shop-manage/ItemKill";
import  Condition  from "alinesno-ui/packages/Search/Condition"
import {ItemList} from "@/api/shop-manage/Item";

export default {
  name: "ItemKill",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 状态
      statusOptions:[] ,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待秒杀商品表格数据
      ItemKillList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemId: null,
        total: null,
        startTime: null,
        endTime: null,
        isActive: null,
        itemName:null,
      },
      // 查询参数配置对象
      queryParamsConfig: {
        itemId: Condition.like(),
        total: Condition.like(),
        startTime: Condition.like(),
        endTime: Condition.like(),
        isActive: Condition.like(),
        itemName: Condition.like()
      },
      //状态
      ActiveOptions: [{
        value: 1,
        label: '上架'
      }, {
        value: 0,
        label: '下架'
      }],

      //添加秒杀商品
      itemTotal:0,
      seckillProduct:null,
      itemTitle:"添加秒杀商品",
      itemOpen:false,
      itemList:null,
      productTitle:"添加秒杀产品",
      // 搜索参数
      searchParams:[] ,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemId: [
          { required: true, message: "商品id不能为空", trigger: "blur" }
        ],
        total: [
          { required: true, message: "可被秒杀的总数不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "秒杀开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "秒杀结束时间不能为空", trigger: "blur" }
        ],
        isActive: [
          { required: true, message: "是否有效不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建的时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();

   this.getItemList();

  },
  methods: {
    /** 查询待秒杀商品列表 */
    getList() {
        this.searchParams = this.searchParam(this.queryParamsConfig, this.queryParams);
        this.loading = true;
      listItemKill(this.searchParams).then(response => {
        this.ItemKillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //获取商品列表
    getItemList(){
      ItemList().then((res)=>{
        this.itemList=res.rows;
        this.itemTotal=res.total;
      });
      console.log('this.itemTotal',this.itemTotal);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemId: null,
        total: null,
        startTime: null,
        endTime: null,
        isActive: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // 获取参数
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    //商品多选
    handleSelectionChangeProduct(selection) {
      this.seckillProduct = selection.map(item => item)
      console.log('this.seckillProduct', this.seckillProduct);
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getItemList();
      //this.open = true;
      this.itemTitle="添加待秒杀商品";
      this.itemOpen=true;
      this.title = "添加待秒杀商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const itemId = row.id || this.ids
      getItemKill(itemId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改待秒杀商品";
      });
    },
    /**
     * 加入秒杀操作
     */
    addSecKill() {
      this.$confirm('是否确认添加所选择的商品?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.itemOpen = false;
        return newAddSeckillGoods(this.seckillProduct);
      }).then(() => {
        this.getList();
        this.itemOpen = false;
        this.msgSuccess("添加成功");
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItemKill(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItemKill(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itemIds = row.id || this.ids;
      this.$confirm('是否确认删除待秒杀商品编号为"' + itemIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delItemKill(itemIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 状态修改 **/
    handleStatusChange(row) {
      return changeStatusItemKill(row.id, row.status).then(response=>{
        if(response.code == 200){
          this.msgSuccess("操作成功");
        }
      });
    },
    /** 修改字段状态 **/
    chanageFile(value , filed , id){
      return changeItemKillField(value , filed , id).then(response =>{
        if(response.code == 200){
          this.msgSuccess("操作成功");
        }
      }) ;
    } ,
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有待秒杀商品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportItemKill(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    formatterClassify(row) {
      let index = this.itemList.findIndex(item => item.id === row.itemId);
      return index === -1 ? "--" : this.itemList[index].name;
    }
  }
};
</script>
